/**

@description : 使用StandardSetController来进行分页
@last modified on : 2023/08/31
@last modified by : SalesforceGogogo.com
**/
public with sharing class BadPaging {
    // 検索结果数量
    public Integer searchCount { get; set; }
    public Integer viewrecCount { get; set; }
    // 搜索Leads列表
public List<Lead> searchLeadList { get; set; }      

// 下一页、上一页相关项
// 用于分页的StandardSetController
public ApexPages.StandardSetController ssController{get; set;}  
public Integer pagescroll { get; set; }             // 用户保存的滚动信息
public Integer lastViewCallListPage { get; set; }   // 用户最后查看的页数
public String lastViewCallList { get; set; }        // 用户最后查看的呼叫列表
private static final Integer PAGE_SIZE = 50;     // 每页显示的记录数

// 分页信息
public Integer currentPage {get; private set;}          // 当前选中的页
public Integer totalPage {get; private set;}            // 总页数
public Integer intSearchLeadListSize { get; set; }          // 搜索Leads列表的大小
public Integer intmyLeadListSize { get; set; }              // 当前显示的列表大小
public Integer firstNum { 
    get{
        if(currentPage != null){
            return PAGE_SIZE * (currentPage - 1) + 1;
        }
        
        return 0;
    }
    set;
}

//-----------------------------------------
// 构造函数
//-----------------------------------------
public BadPaging(){
	searchWithPagnation();
}

public void searchWithPagnation(){
    this.searchCount = 0;
    
    String leadViewQuery = 
       'Select                            ' + 
       '    Id                            ' + 
       '    ,Name                         ' + 
       '    ,Phone                        ' + 
       '    ,MobilePhone                  ' + 
       '    ,State                        ' + 
       '    ,City                         ' + 
       '    ,Street                       ' + 
       '    ,CreatedDate                  ' + 
       '    ,FirstName                    ' + 
       '    ,LastName                     ' + 
       '    ,Company                      ' + 
       '    ,ConvertedAccountId           ' +
       'From                              ' + 
       '    Lead                          ' + 
       'Where                             ' + 
       '    IsDeleted = false             ' ;
       
    String leadCntQuery = 
       'Select                            ' + 
       '    Count(Id) cnt                 ' + 
       'From                              ' + 
       '    Lead                          ' + 
       'Where                             ' + 
       '    IsDeleted = false             ' ;
    
	// 如果需要根据相关数据进行筛选，则从相关数据中进行搜索
	String whereStr = '';
	leadViewQuery = leadViewQuery + 'LIMIT 10000';
	System.debug('■leadViewQuery---------------->'+ leadViewQuery);
	
	//List<Lead> searchLeadListQuery = Database.query(leadViewQuery);
	//System.debug('■searchLeadListQuery---------------->'+ searchLeadListQuery);
	
	this.searchLeadList = Database.query(leadViewQuery);
	this.intSearchLeadListSize = this.searchLeadList.Size();
	System.debug('■searchLeadList.Size()---------------->'+ this.searchLeadList.Size());
	
	
	leadCntQuery = leadCntQuery + whereStr;
    AggregateResult[] groupedResults = Database.query(leadCntQuery);
    
    // 保存搜索结果的数量
    this.searchCount = Integer.valueOf(groupedResults.get(0).get('cnt'));
    //System.debug('■searchCount---------------->'+ searchCount);
	
	if(this.searchCount >= 50){
        this.viewrecCount = 50;
    }else{
        this.viewrecCount = this.searchCount;
    }
    
    changePage();
}

//-----------------------------
    // 改ページ設定初期化
    //-----------------------------
    public PageReference changePage() {
        System.debug('改ページ設定変更');
        ssController = new ApexPages.StandardSetController(this.searchLeadList);
        ssController.setPageSize(PAGE_SIZE);            
        
        // ページの総数を計算
        // レコード数 / ページサイズで計算、小数部分は切り上げ
        totalPage = (Integer)Math.ceil((Decimal)ssController.getResultSize() / PAGE_SIZE);
        System.debug('■totalPage---------------->'+ totalPage);
        
        
        //System.debug('保存されたコールリスト名:'+ this.lastViewCallList);
        
        // 現在のページ番号を取得
        if(
            this.lastViewCallListPage > 1 
            && this.lastViewCallListPage < = totalPage){
            currentPage = this.lastViewCallListPage;
            ssController.setpageNumber(currentPage);
        } else {
            currentPage = ssController.getPageNumber();
        }

        
        this.searchLeadList = getSearchLeadList();  
        System.debug('■searchLeadList.size()---------------->'+ this.searchLeadList.size());
        
        return null;
    }
    
    //---------------------------------------
    // 前へボタンが有効か
    //---------------------------------------
    public Boolean getEnablePrev(){
        
        System.debug('■ssController---------------->'+ ssController);
        if(ssController != null){
            System.debug('■ssController.getHasPrevious()---------------->');
            
            //List<Lead> myLeadList = (List<Lead>)ssController.getRecords();
            //System.debug('■myLeadList---------------->'+ myLeadList.size());
        
        
            return ssController.getHasPrevious();
        }else{
            System.debug('■getEnablePrev()---------------->null');
            return null;
        }
    }
    
    //---------------------------------------
    // 次へボタンが有効か
    //---------------------------------------
    public Boolean getEnableNext(){
        if(ssController != null){
            return ssController.getHasNext();
        }else{
            return null;
        }
    }
    
    //---------------------------------------
    // 次へボタンクリックアクション
    //---------------------------------------
    public void next() {
        ssController.next();
        currentPage = ssController.getPageNumber();
        
        this.searchLeadList = getSearchLeadList();  
    }
    
    //---------------------------------------
    // 前へボタンクリックアクション
    //---------------------------------------
    public void previous() {
        ssController.previous();    
        currentPage = ssController.getPageNumber();
        
        this.searchLeadList = getSearchLeadList();
    }
    
    
    //---------------------------------------
    // 現ページのレコードを取得する
    //---------------------------------------
    public List<Lead> getSearchLeadList(){
        System.debug('現ページのレコードを取得する');
        
        List<Lead> myLeadList = (List<Lead>)ssController.getRecords();
        System.debug('■myLeadList---------------->'+ myLeadList.size());
        
        intmyLeadListSize = myLeadList.size();
        
        
        return (List<Lead>)ssController.getRecords();
    }
}